import AbsenceAudit from "@/views/absence/AbsenceAudit.vue";
import AbsenceList from "@/views/absence/AbsenceList.vue";
import DeptList from "@/views/sys/DeptList.vue";
import DictTypeList from "@/views/travel/DictTypeList.vue";
import Login from "@/views/sys/Login.vue";
import LostReportList from "@/views/report/LostReportList.vue";
import Menus from "@/views/sys/Menus.vue";
import MonthView from "@/views/report/MonthView.vue";
import PersonalReport from "@/views/report/PersonalReport.vue";
import ProjectList from "@/views/sys/ProjectList.vue";
import ReportMenu from "@/views/report/ReportMenu.vue";
import TicketList from "@/views/travel/TicketList.vue";
import TravelApproval from "@/views/travel/TravelApproval.vue";
import TravelList from "@/views/travel/TravelList.vue";
import AddorEditBApplication from "@/views/wzgl/AddorEditBApplication.vue";
import AddorEditGoods from "@/views/wzgl/AddorEditGoods.vue";
import AddorEditRApplication from "@/views/wzgl/AddorEditRApplication.vue";
import BorrowApplicationManager from "@/views/wzgl/BorrowApplicationManager.vue";
import CheckBApplication from "@/views/wzgl/CheckBApplication.vue";
import CheckBApplicationManager from "@/views/wzgl/CheckBApplicationManager.vue";
import CheckRApplication from "@/views/wzgl/CheckRApplication.vue";
import CheckRApplicationManager from "@/views/wzgl/CheckRApplicationManager.vue";
import GoodsLogManager from "@/views/wzgl/GoodsLogManager.vue";
import GoodsManager from "@/views/wzgl/GoodsManager.vue";
import ReturnApplicationManager from "@/views/wzgl/ReturnApplicationManager.vue";
import ViewRApplication from "@/views/wzgl/ViewRApplication.vue";
import Vue from "vue";
import VueRouter from "vue-router";
import CharacterManger from "@/views/cgxm/characterManger.vue";
import AddSupplier from "@/views/cgxm/addSupplier.vue";
import AddProType from "@/views/cgxm/addProType.vue";
import Cgglemp from "@/views/cgxm/cgglemp.vue";
import MenuManager from "@/views/menu/MenuManager.vue";
import AddorEditMenu from "@/views/menu/AddorEditMenu.vue";
import First from "@/views/sys/First.vue";
import EmployeeSummary from "@/views/order/EmployeeSummary.vue";
import DeptSummary from "@/views/order/DeptSummary.vue";
import FoodManagement from "@/views/order/FoodManagement.vue";
import HistoryOrders from "@/views/order/HistoryOrders.vue";
import EmpManager from "@/views/order/EmpManager.vue";
import UserManager from "@/views/order/UserManager.vue";
import EmpList from "@/views/order/EmpList.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    name: "Login",
    component: Login,
  },
  {
    path: "/menus",
    name: "Menus",
    component: Menus,
    children: [
      { path: "/",component:First },
      { path: "/first",name:'first',component:First},
      { path: "/absencelist", name:'absenceback', component: AbsenceList },
      { path: "/absenceaudit",name:'absenceaudit', component: AbsenceAudit },
      { path: "/ticketList", component: TicketList },
      { path: "/travelList",name:'travelback', component: TravelList },
      { path: "/travelApproval",name:'travelaudit', component: TravelApproval },
      { path: "/dictTypeList", component: DictTypeList },
      { path: "/reportMenu",name:'report', component: ReportMenu },
      {
        path: "/personalReport",
        component: PersonalReport,
        beforeEnter: (to, from, next) => {
          if (to.query.listForm === "check" || to.query.listForm === "dept") {
            const roleStr = localStorage.getItem("roleid") || "";
            const role = roleStr
              .split(",")
              .map(Number)
              .filter((id) => !isNaN(id));
            if (!role.includes(3)) {
              next("/reportMenu");
              return;
            }
          }
          next();
        },
      },
      {
        path: "/lostReportList",
        component: LostReportList,
        beforeEnter: (to, from, next) => {
          if (to.query.listForm === "check") {
            const roleStr = localStorage.getItem("roleid") || "";
            const role = roleStr
              .split(",")
              .map(Number)
              .filter((id) => !isNaN(id));
            if (!role.includes(3)) {
              next("/reportMenu");
              return;
            }
          }
          next();
        },
      },
      {
        path: "/monthView",
        component: MonthView,
        beforeEnter: (to, from, next) => {
          const roleStr = localStorage.getItem("roleid") || "";
          const role = roleStr
            .split(",")
            .map(Number)
            .filter((id) => !isNaN(id));
          if (!role.includes(3)) {
            next("/reportMenu");
            return;
          }
          next();
        },
      },
      { path: "/deptList", component: DeptList },
      { path: "/projectList", component: ProjectList },
      {
        path: "/goodsManager",
        component: GoodsManager,
      },
      {
        path: "/goodsLogManager",
        component: GoodsLogManager,
      },
      {
        path: "/borrowApplicationManager",
        name:'borrowback',
        component: BorrowApplicationManager,
      },
      {
        path: "/returnApplicationManager",
        name:'returnback',
        component: ReturnApplicationManager,
      },
      {
        path: "/checkBApplicationManager",
        name:'borrowaudit',
        component: CheckBApplicationManager,
      },
      {
        path: "/checkRApplicationManager",
        name:'returnaudit',
        component: CheckRApplicationManager,
      },
      {
        path: "/addorEditGoods",
        component: AddorEditGoods,
      },
      {
        path: "/addorEditBApplication",
        component: AddorEditBApplication,
      },
      {
        path: "/checkBApplcation",
        component: CheckBApplication,
      },
      {
        path: "/addorEditRApplication",
        component: AddorEditRApplication,
      },
      {
        path: "/viewRApplication",
        component: ViewRApplication,
      },
      {
        path: "/checkRApplication",
        component: CheckRApplication,
      },
      { path: "/cgglemp", component: Cgglemp, name: "cg" },
      { path: "/addProType", component: AddProType, name: "采购类型维护" },
      { path: "/addSupplier", component: AddSupplier, name: "供应商信息维护" },
      { path: "/characterManger", component: CharacterManger },
      {
        path: "/menuManager",
        name: "menuManager",
        component: MenuManager,
      },
      {
        path: "/addorEditMenu",
        name: "addorEditMenu",
        component: AddorEditMenu,
      },
      {path: '/employeesummary',component: EmployeeSummary,name:'orderback'},
      {path: '/deptsummary', component: DeptSummary,name:'orderaudit'},
      { path: '/foodmanagement',component: FoodManagement},
      {path:'/historyorders',component:HistoryOrders},
      {path:'/empmanager',component:EmpManager},
      {path:'/usermanager',component:UserManager},
      {path:'/emplist',component:EmpList}
      
    ],
  },
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.name !== "Login") {
    const token = localStorage.getItem('jwt')
    const userid = localStorage.getItem('userid')
    debugger
    
    if (!userid) {
      next('Login')
    } else {
      next()
    }
  } else {
    localStorage.clear()
    next()
  }
})

export default router;
